package br.com.wagnerpaz.battlemaniacs.generic;
import java.io.IOException;
import java.net.ServerSocket;


/**
 * @author Wagner Paz
 */
public abstract class AbstractServer implements Runnable
{
	protected ServerSocket server;
	private   boolean      running;
	private   Thread       thread;
	
	protected int port;
	
	public AbstractServer(int port)
	{
		this.port = port;
	}
	
	public void start()
		throws IOException
	{
		server = new ServerSocket(port);
		
		running = true;
		
		thread = new Thread(this);
		thread.start();
	}
	
	public void stop()
	{
		try
		{
			running = false;
			server.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void run()
	{
		while(running)
		{
			service();
		}
	}
	
	public abstract void service();

	public Thread getThread()
	{
		return thread;
	}
}